package com.hjm.common.utils.yh;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

	public static String getCurrentDateTime(String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String strDate = sdf.format(new Date());
		return strDate;
	}

	public static String toStrDate(String time, String date) throws Exception {
		StringBuffer dateStr = new StringBuffer();
		dateStr.append(getCurrentDateTime("yyyy"));
		dateStr.append("-");
		dateStr.append(date.substring(0, 2));
		dateStr.append("-");
		dateStr.append(date.substring(2));
		dateStr.append(" ");
		dateStr.append(time.substring(0, 2));
		dateStr.append(":");
		dateStr.append(time.substring(2, 4));
		dateStr.append(":");
		dateStr.append(time.substring(4));
		return dateStr.toString();
	}

	public static String sub12(String date) throws Exception {
		String[] dates = date.split("-");
		return (DateUtil.appendField(dates[1], dates[2]));
	}


	public static String sub13(String time) throws Exception {
		String[] times = time.split(":");
		return (DateUtil.appendField(times[0], times[1], times[2]));
	}


	public static String appendField(Object ... params) {
		StringBuffer sbf = new StringBuffer();
		for (Object str : params) {
			sbf.append(str);
		}
		return sbf.toString();
	}


	public static long hasMillisecond() throws Exception {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
		Date endDate = dft.parse(dft.format(date.getTime()));
		long between = 0;
		Date begin = dfs.parse(dfs.format(new Date()));
		Date end = dfs.parse(dft.format(endDate) + " 01:00:00.000");
		between = (end.getTime() - begin.getTime());
		return between;
	}
}
